package com.blueheart.music.controller;

import com.blueheart.music.common.ResultUtil;
import com.blueheart.music.consts.MyConst;
import com.blueheart.music.exception.BaseExceptionEnum;
import com.blueheart.music.model.pojo.Song;
import com.blueheart.music.service.SongService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/*
歌曲控制层
 */
@RestController
public class SongController {
    @Autowired
    private SongService songService;

    /**
     * 增加歌曲
     * @return
     */
    @PostMapping("/song/addSong")
    public ResultUtil addSong(HttpServletRequest request){
        String name=request.getParameter(MyConst.SING_NAME).trim();
        String singerId=request.getParameter(MyConst.SINGER_ID).trim();
        String pic=request.getParameter(MyConst.SING_PIC).trim();
        String introduction=request.getParameter(MyConst.SING_INTRODUCTION).trim();
        String lyric=request.getParameter(MyConst.SONG_LYRIC).trim();
        String url=request.getParameter(MyConst.URL).trim();
        if (StringUtils.isEmpty(name)||StringUtils.isEmpty(url)){
            return ResultUtil.error(BaseExceptionEnum.PARAM_ERROR);
        }
        if (songService.checkName(name)==null){
            Song song = new Song();
            song.setSingerId(new Integer(singerId));
            song.setName(name);
            song.setIntroduction(introduction);
            song.setLyric(lyric);
            song.setUrl(url);
            song.setPic(pic);
            songService.addSong(song);
            return ResultUtil.success();
        }
       return ResultUtil.error(BaseExceptionEnum.SONG_EXIST);
    }
    /**
     * 删除歌曲
     */
    @PostMapping("/song/deleteSong")
    public ResultUtil deleteSong(@RequestParam("id") Integer id){
        if (id>0){
            Boolean flag = songService.removeSong(id);
            if (flag){
                return ResultUtil.success();
            }
            return ResultUtil.error(BaseExceptionEnum.REMOVE_ERROR);
        }
        return ResultUtil.error(BaseExceptionEnum.PARAM_ERROR);
    }
    /**
     * 更新歌曲
     */
    @PostMapping("/song/updateSong")
    public ResultUtil updateSong(HttpServletRequest request){
        String id=request.getParameter(MyConst.SING_ID).trim();
        String singerId=request.getParameter(MyConst.SINGER_ID).trim();
        String name=request.getParameter(MyConst.SING_NAME).trim();
        String pic=request.getParameter(MyConst.SING_PIC).trim();
        String introduction=request.getParameter(MyConst.SING_INTRODUCTION).trim();
        String lyric=request.getParameter(MyConst.SONG_LYRIC).trim();
        String url=request.getParameter(MyConst.URL).trim();
        Song song = new Song();
        song.setId(new Integer(id));
        song.setSingerId(new Integer(singerId));
        song.setName(name);
        song.setIntroduction(introduction);
        song.setLyric(lyric);
        song.setUrl(url);
        song.setPic(pic);
        songService.reviseSong(song);
        return ResultUtil.success();
    }
    /**
     * 查询全部歌曲
     */
    @GetMapping("/song/findAll")
    public ResultUtil findAll(){
        List<Song> all = songService.findAll();
        if (all.size()>0){
            return ResultUtil.success(all);
        }
       return  ResultUtil.error(BaseExceptionEnum.SELETE_ERROR);
    }
    /**
     * 根据歌名模糊查询歌曲
     */
    @PostMapping("/song/findByName")
    public ResultUtil findByName(@RequestParam("name") String name){
       if (StringUtils.isEmpty(name)){
           return ResultUtil.error(BaseExceptionEnum.SELETE_ERROR);
       }
        List<Song> byName = songService.findByName("%"+name+"%");
        if (byName.size()>0){
            return ResultUtil.success(byName);
        }
        return  ResultUtil.error(BaseExceptionEnum.SELETE_ERROR);
    }
    /**
     * 根据歌手id查询歌曲
     */
    @PostMapping("/song/findBySingerId")
    public ResultUtil findBySingerId(@RequestParam("id") Integer id){
        List<Song> bySingerId = songService.findBySingerId(id);
        if (bySingerId.size()>0){
            return ResultUtil.success(bySingerId);
        }
        return  ResultUtil.error(BaseExceptionEnum.SELETE_ERROR);
    }
    /**
     * 根据歌曲id查询歌曲
     */
    @PostMapping("/song/findById")
    public ResultUtil findById(@RequestParam("id") Integer id){
        Song byId = songService.findById(id);
        if (byId!=null){
            return ResultUtil.success(byId);
        }
        return  ResultUtil.error(BaseExceptionEnum.SELETE_ERROR);
    }

    /**
     * 通过名字查询id
     * @param name
     * @return
     */
    @PostMapping("/song/findToId")
    public ResultUtil findToId(@RequestParam("name") String name){
        Song toId = songService.findToId(name);
        if (toId!=null){
            return ResultUtil.success(toId);
        }
        return  ResultUtil.error(BaseExceptionEnum.SELETE_ERROR);
    }
}
